/*
 Copyright (C) 2008  Maurizio Monticelli

 This program is free software; you can redistribute it and/or modify it under the terms of the 
 GNU General Public License as published by the Free Software Foundation; either version 2 of the 
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write 
 to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
*/

package ch.justdoit.andriodrpncalc.view.buttons;

import ch.justdoit.andriodrpncalc.model.Model;
import ch.justdoit.andriodrpncalc.model.command.ICommand;
import android.content.Context;
import android.view.View;

public class BaseButtonMultifunction extends BaseImageButton implements IThirdFunctionButton {


	private ICommand firstCommand;
	private int firstImage;
	private ICommand secondCommand;
	private int secondImage;
	private ICommand thirdCommand;
	private int thirdImage;
	private int command;
	
	public BaseButtonMultifunction(Context context, Model _model) {
		super(context, _model);
		setOnClickListener(onClickListener);
		command = 0;
	}
	
	public void registerFirst(ICommand _firstCommand, int _firstImage) {
		firstCommand = _firstCommand;
		firstImage   = _firstImage;
		setImageResource(firstImage);
	}

	public void registerSecond(ICommand _secondCommand, int _secondImage) {
		secondCommand = _secondCommand;
		secondImage   = _secondImage;
	}
	
	public void registerThird(ICommand _thirdCommand, int _thirdImage) {
		thirdCommand = _thirdCommand;
		thirdImage   = _thirdImage;
	}

	public void onFirstFunction () {
		setImageResource(firstImage);
		command = 0;
	}
	
	public void onSecondFunction () {
		setImageResource(secondImage);
		command = 1;
	}

	public void onThirdFunction() {
		setImageResource(thirdImage);
		command = 2;
	}


	private OnClickListener onClickListener = new OnClickListener()
    {
        public void onClick(View v)
        {
        	if (command == 2) {
	    		if (thirdCommand != null) {
	    			model.mCommandManager.submit(thirdCommand);
	    		}
        	}
        	if (command == 1) {
	    		if (secondCommand != null) {
	    			model.mCommandManager.submit(secondCommand);
	    		}
        	}
        	if (command == 0) {
	    		if (firstCommand != null) {
	    			model.mCommandManager.submit(firstCommand);
	    		}
        	}

        }
    };

}
